﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GrainManage.Server.Factory;
using System.Text;
using GrainManage.Entities.Weather;
using System.Text.RegularExpressions;
using GrainManage.Entities;

namespace GrainManage.Server.Business
{
    public class WeatherBusiness : BaseBusiness, GrainManage.Interface.IWeather
    {
        private const string ZH_CN = "^[\u4E00-\u9FA5]+$";//匹配简体中文
        private const string PROPERTY_NAME = "weatherinfo";

        //************************************************POST************************************************
        public OutputGetYahooWeather GetYahooWeather(InputGetYahooWeather input)
        {
            var result = new OutputGetYahooWeather();
            try
            {
                var url = ObjectFactory.GetAppConfig("YahooWeatherApiUrl");
                var codedCity = System.Web.HttpUtility.UrlEncode(input.City, UTF8Encoding.UTF8);
                result.Weather = GrainManage.Server.Tool.WebUtil.GetResultFromJsonByGet<YahooWeather>(url, "city=" + codedCity);
                if (result.Weather == null)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetChinaRootWeather GetChinaRootWeather(InputGetChinaRootWeather input)
        {
            OutputGetChinaRootWeather result = new OutputGetChinaRootWeather();
            try
            {
                var url = ObjectFactory.GetAppConfig("ChinaWeatherApiUrl");
                url = url.Replace("#pinyin#", "china");
                result.RootWeather = GrainManage.Server.Tool.WebUtil.GetResultFromXmlByGet<ChinaRootWeather>(url);
                if (result.RootWeather == null)
                {
                    SetResponse(StatusCode.NoData, result);
                }
                else
                {
                    SetResponse(StatusCode.Success, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }
            return result;
        }

        public OutputGetChinaCityWeather GetChinaCityWeather(InputGetChinaCityWeather input)
        {
            OutputGetChinaCityWeather result = new OutputGetChinaCityWeather();
            try
            {
                var pinyin = GetPinyin(input.NameOrPinyin, input);
                if (string.IsNullOrEmpty(pinyin) || pinyin == "china")
                {
                    SetResponse(StatusCode.CityNotFound, result);
                }
                else
                {
                    var url = ObjectFactory.GetAppConfig("ChinaWeatherApiUrl");
                    url = url.Replace("#pinyin#", pinyin);
                    result.CityWeather = GrainManage.Server.Tool.WebUtil.GetResultFromXmlByGet<ChinaCityWeather>(url);
                    if (result.CityWeather == null)
                    {
                        SetResponse(StatusCode.NoData, result);
                    }
                    else
                    {
                        SetResponse(StatusCode.Success, result);
                    }
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }
            return result;
        }

        public OutputGetChinaCurrentWeather GetChinaCurrentWeather(InputGetChinaCurrentWeather input)
        {
            OutputGetChinaCurrentWeather result = new OutputGetChinaCurrentWeather();
            try
            {
                var code = GetCode(input.NameOrCode, input);
                if (string.IsNullOrEmpty(code))
                {
                    SetResponse(StatusCode.CityNotFound, result);
                }
                else
                {
                    var url = ObjectFactory.GetAppConfig("ChinaCurrentWeatherApiUrl");
                    url = url.Replace("#code#", code);
                    result.CurrentWeather = GrainManage.Server.Tool.WebUtil.GetResultFromJsonByGet<ChinaCurrentWeather>(url, string.Empty, PROPERTY_NAME);
                    if (result.CurrentWeather == null)
                    {
                        SetResponse(StatusCode.NoData, result);
                    }
                    else
                    {
                        SetResponse(StatusCode.Success, result);
                    }
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }
            return result;
        }

        public OutputGetChinaNext7DayWeather GetChinaNext7DayWeather(InputGetChinaNext7DayWeather input)
        {
            OutputGetChinaNext7DayWeather result = new OutputGetChinaNext7DayWeather();
            try
            {
                var code = GetCode(input.NameOrCode, input);
                if (string.IsNullOrEmpty(code))
                {
                    SetResponse(StatusCode.CityNotFound, result);
                }
                else
                {
                    var url = ObjectFactory.GetAppConfig("ChinaNext7DayWeatherApiUrl");
                    url = url.Replace("#code#", code);
                    result.Next7DayWeather = GrainManage.Server.Tool.WebUtil.GetResultFromJsonByGet<ChinaNext7DayWeather>(url, string.Empty, PROPERTY_NAME);
                    if (result.Next7DayWeather == null)
                    {
                        SetResponse(StatusCode.NoData, result);
                    }
                    else
                    {
                        SetResponse(StatusCode.Success, result);
                    }
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }
            return result;
        }

        public OutputGetImageUrl GetImageUrl(InputGetImageUrl input)
        {
            OutputGetImageUrl result = new OutputGetImageUrl();
            try
            {
                string url = ObjectFactory.GetAppConfig("ChinaWeatherImageUrl");
                string flag = "#name#";
                switch (input.ImageType)
                {
                    case ImageType.Big:
                        result.ImageUrl = url.Replace(flag, "a" + input.WeatherState);
                        break;
                    case ImageType.Medium:
                        result.ImageUrl = url.Replace(flag, "b" + input.WeatherState);
                        break;
                    case ImageType.Small:
                        result.ImageUrl = url.Replace(flag, "c" + input.WeatherState);
                        break;
                    case ImageType.DayHighlight:
                        result.ImageUrl = url.Replace(flag, "d" + input.WeatherState);
                        break;
                    case ImageType.NightHighlight:
                        result.ImageUrl = url.Replace(flag, "n" + input.WeatherState);
                        break;
                }
                SetResponse(StatusCode.Success, result);
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        public OutputGetImageFile GetImageFile(InputGetImageFile input)
        {
            OutputGetImageFile result = new OutputGetImageFile();
            try
            {
                string url = ObjectFactory.GetAppConfig("ChinaWeatherImageUrl");
                string flag = "#name#";
                switch (input.ImageType)
                {
                    case ImageType.Big:
                        url = url.Replace(flag, "a" + input.WeatherState);
                        break;
                    case ImageType.Medium:
                        url = url.Replace(flag, "b" + input.WeatherState);
                        break;
                    case ImageType.Small:
                        url = url.Replace(flag, "c" + input.WeatherState);
                        break;
                    case ImageType.DayHighlight:
                        url = url.Replace(flag, "d" + input.WeatherState);
                        break;
                    case ImageType.NightHighlight:
                        url = url.Replace(flag, "n" + input.WeatherState);
                        break;
                }
                result.ImageFile = GrainManage.Server.Tool.WebUtil.DownloadFile(url);
                if (result.ImageFile != null)
                {
                    SetResponse(StatusCode.Success, result);
                }
                else
                {
                    SetResponse(StatusCode.NoData, result);
                }
            }
            catch (Exception e)
            {
                SetResponse(StatusCode.Failed, result, e.Message);
                WriteLog(System.Reflection.MethodBase.GetCurrentMethod(), e, input, result);
            }

            return result;
        }

        #region Private

        private static string GetPinyin(string nameOrPinyin, BaseInputModel input)
        {
            string result = string.Empty;
            if (string.IsNullOrWhiteSpace(nameOrPinyin))
            {
                result = string.Empty;
            }
            else
            {
                nameOrPinyin = nameOrPinyin.Trim();
                if (Regex.IsMatch(nameOrPinyin, ZH_CN))
                {
                    var service = ObjectFactory.GetWeatherService(input);
                    result = service.GetPinyinByName(nameOrPinyin, false);
                }
                else
                {
                    result = nameOrPinyin.ToLower();
                }
            }
            return result;
        }

        private static string GetCode(string nameOrCode, BaseInputModel input)
        {
            string result = string.Empty;
            if (string.IsNullOrWhiteSpace(nameOrCode))
            {
                result = string.Empty;
            }
            else
            {
                nameOrCode = nameOrCode.Trim();
                if (Regex.IsMatch(nameOrCode, ZH_CN))
                {
                    var service = ObjectFactory.GetWeatherService(input);
                    result = service.GetPinyinByName(nameOrCode, false);
                }
                else
                {
                    result = nameOrCode;
                }
            }
            return result;
        }

        private static bool IsValidateOnline
        {
            get
            {
                try
                {
                    Uri MyUri = new Uri("http://m.weather.com.cn");
                    System.Net.WebRequest wb = System.Net.WebRequest.Create(MyUri);
                    wb.Proxy = new System.Net.WebProxy();
                    using (System.Net.WebResponse wsp = wb.GetResponse()) { };
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        #endregion

    }
}